package com.nowcoder.community;


import com.nowcoder.community.dao.DiscussPostMapper;
import com.nowcoder.community.dao.UserMapper;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.utils.CommunityUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class testUserMapper {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Test
    public void testSelectsql(){
        User user = userMapper.selectUserById(11);
        System.out.println(user);
        System.out.println(userMapper.selectUserByName("nowcoder12"));
        System.out.println(userMapper.selectUserByEmail("nowcoder13@sina.com"));
    }

    @Test
    public void testInsertsql(){
        User user = new User();
        user.setUsername("mwx");
        user.setPassword("123456");
        user.setSalt("csda5");
        user.setEmail("mwx@qq.com");
        user.setHeaderUrl("http://www.nowcoder.com/109.png");
        user.setCreateTime(new Date());
        int row = userMapper.insertUser(user);
        System.out.println(row);
    }


    @Test
    public void insert(){

        //根据随机盐重新组装密码,在MD5加密

        User user = new User();
        user.setUsername("mwx");
        user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
        user.setPassword(CommunityUtil.md5("123456" + user.getSalt()));
        user.setEmail("mwx@qq.com");
        user.setHeaderUrl("http://www.nowcoder.com/109.png");
        user.setCreateTime(new Date());
        int row = userMapper.insertUser(user);
        System.out.println(row);

    }

    @Test
    public void testUpdatesql(){
//        int i = userMapper.updateStatus(150, 1);
//        System.out.println(i);
//
//        int j = userMapper.updatePassword(150,"654321");
//        System.out.println(j);

        int k = userMapper.updateHeader(101,"http://www.nowcoder.com/111.png");
        System.out.println(k);
    }



    @Test
    public void testSelectPosts() {
        List<DiscussPost> list = discussPostMapper.selectDiscussPost(149, 0, 10,0);
        for(DiscussPost post : list) {
            System.out.println(post);
        }

        int rows = discussPostMapper.selectDiscussPostRows(149);
        System.out.println(rows);
    }


    @Test
    public void t(){
        String fileName = "sadasfasf.png";
        String suffix = fileName.substring(fileName.lastIndexOf('.') + 1);
        System.out.println(suffix);
        fileName = CommunityUtil.generateUUID() + suffix;
        System.out.println(fileName);
    }

}
